<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<html>
<head>
<title>_${customer.customerName}_入库单审核</title>
<script type="text/javascript">
	$(function() {
		$("#reason").val('${auditInfo.info}');
	});

	//审核
	function audit(auditType) {
	
	   $("#pass_btn").attr("disabled",true);
	   $("#notpass_btn").attr("disabled",true);

		var reason = $("#reason").val().trim();
		if (auditType == 'notpass') { //审核不通过，验证审核意见是否填写
			if (reason == "" || reason == null) {
				alert("审核驳回，需要填写审核意见！");
				$("#reason").focus();
				$("#pass_btn").attr("disabled",false);
	            $("#notpass_btn").attr("disabled",false);
				return;
			}
		}

		$.ajax({
			type : 'post',
			url : '${ctx}/cusInstorageAudit/ajaxAudit',
			data : {
				auditType : auditType,
				billCode : '${bill.instorageCode}',
				reason : reason
			},
			dataType : 'json',
			success : function(data) {
				if (data == true) {
					alert("审核操作成功！");
					location.reload();
					
					if(auditType=='pass'){
					  $("#tab2").class("tab-pane active");
					}
					
				} else {
				    $("#pass_btn").attr("disabled",false);
	                $("#notpass_btn").attr("disabled",false);
					alert("网络繁忙，请稍后重试...");
				}
			},
			error : function() {
			    $("#pass_btn").attr("disabled",false);
	            $("#notpass_btn").attr("disabled",false);
				alert("网络繁忙，请稍后重试...");
			}
		});

	}

	//美化车载信息
	function formatCarInfo(carInfo) {
		var carInfoArray = carInfo.split(";");
		var temp = '';
		for ( var i = 0; i < carInfoArray.length; i++) {
			var car = carInfoArray[i];
			if (car == null || car == '') {
			} else {
				temp += "【" + car + "(件)】";
			}
		}
		return temp;
	}
	
	//指派任务
	function task(taskId){
	
	  var btn_id="#btn_"+taskId;
	  var worker_id="#worker_"+taskId;
	  var status_id="#status_"+taskId;
	  var commander_id="#commander_"+taskId;
	  
	  var jobuuid=$(worker_id).val();

	  if(jobuuid==''){
	    alert("请选择执行人");
	    $(worker_id).focus();
	    return;
	  }
	  
	  $.ajax({
			type : 'post',
			url : '${ctx}/cusInstorageAudit/ajaxTask',
			data : {
				jobuuid : jobuuid,
				taskId : taskId
			},
			dataType : 'json',
			success : function(data) {
			    var msg=data.msg;
				if (msg == "success") {
					 alert("任务指派成功！");
					 $(btn_id).attr("disabled", true);
	                 $(btn_id).attr("class", "btn");
	                 $(btn_id).val("已指派");
	                 $(worker_id).attr("disabled", true);
	                 $(status_id).html("任务待执行...");
					 $(commander_id).html('${curUser.realname}');
				}else if(msg=="token"){
				    alert("此任务已被分配。");
				    location.reload();
				}else {
					alert("网络繁忙，请稍后重试...");
				}
			},
			error : function() {
				alert("网络繁忙，请稍后重试...");
			}
		});
	}
	
	
</script>
</head>

<body>

	<div class="tabbable">
		<ul class="nav nav-tabs">
			<li class="${tab1}"><a href="#tab1" data-toggle="tab">入库单审核<span
					id="fold"></span> </a></li>
			<li class="${tab2}"><a href="#tab2" data-toggle="tab">指派上架任务<span
					id="unfold"></span> </a></li>
		</ul>

		<div class="tab-content">

			<div class="tab-pane ${tab1}" id="tab1">
				<div style="height: 10px;"></div>
				<!-- 登记数据 -->
				<table class="table table-striped table-bordered table-condensed"
					style="width: 95%; margin-top: 0px; margin-left: 20px;">
					<thead>
						<th style="vertical-align: middle; color: white;" colspan="4"
							bgcolor="#8EB4CB">登记数据</th>
					</thead>
					<tr>
						<th style="width: 120px;">入库编号</th>
						<th style="width: 100px;">箱子数量</th>
						<th style="width: 100px;">SKU数量</th>
						<th style="width: 100px;">商品数量</th>
					</tr>
					<tr>
						<td>${bill.instorageCode}</td>
						<td>${bill.boxNum}</td>
						<td>${bill.skuNum}</td>
						<td>${bill.goodsNum}</td>
					</tr>
				</table>

				<!-- 点收数据 -->
				<table class="table table-striped table-bordered table-condensed"
					style="width: 95%; margin-top: 0px; margin-left: 20px;"
					id="count_table">
					<thead>
						<th style="vertical-align: middle; color: white; " colspan="5"
							bgcolor="#8EB4CB"><span>点收数据</span></th>
					</thead>
					<tr>
						<th style="width: 200px;">SKU编码</th>
						<th style="width: 130px;">散货件数量（件）</th>
						<th style="width: 130px;">破损件数量（件）</th>
						<th>整箱件数量（件/箱）</th>
						<th style="width: 130px;">总数量（件）</th>
					</tr>
					<tbody>
						<c:forEach items="${countList}" var="count">
							<tr>
								<td>${count.sku}</td>
								<td>${count.bulkNum}</td>
								<td>${count.errorNum}</td>
								<td><c:if test="${count.wholeStr ne '' }">${count.wholeStr}=${count.wholeNum}
								</td>
								</c:if>
								<c:if test="${count.wholeStr eq '' }">${count.wholeNum}</td>
								</c:if>
								<td>${count.totalNum}</td>
							</tr>
						</c:forEach>
					</tbody>
				</table>
				
				<!-- 审核日志 -->
				<table class="table table-striped table-bordered table-condensed" style="width: 95%; margin-top: 0px; margin-left: 20px;" id="count_table">
					<thead>
						<th style="vertical-align: middle; color: white; " colspan="5" bgcolor="#8EB4CB"><span>审核日志</span></th>
					</thead>
					<tr>
						<th style="width: 200px;">处理时间</th>
						<th style="width: 130px;">处理人</th>
						<th style="width: 130px;">处理内容</th>
						<th>说明</th>
					</tr>
					<tbody>
						<c:forEach items="${logHistoryList}" var="history">
							<tr>
								<td>${history.startTime}</td>
								<td>${history.user.realname}</td>
								<td>
								  <c:if test="${history.workType eq '1'}">入库登记</c:if>
							      <c:if test="${history.workType eq '2'}">点数/质检</c:if> 
							      <c:if test="${history.workType eq '3'}">审核通过</c:if> 
							      <c:if test="${history.workType eq '4'}">审核驳回</c:if> 
								</td>
								<td> ${history.info} </td>
							</tr>
						</c:forEach>
					</tbody>
				</table>

				<!-- 审核操作-->
				<table class="table table-striped table-bordered table-condensed"
					style="width: 95%; margin-top: 0px; margin-left: 20px;"
					id="count_table">
					<thead>
						<th style="vertical-align: middle; color: white;" colspan="4" bgcolor="#8EB4CB">审核操作</th>
					</thead>
					<tr>
						<td>审核意见： <c:if test="${bill.status eq '2' }">
								<textarea rows="3" id="reason" style="width:790px;"
									maxlength="100"></textarea>
							</c:if> <c:if test="${bill.status ne '2' }">
								<textarea rows="3" id="reason" style="width:790px;"
									maxlength="100" readonly="readonly"></textarea>
							</c:if></td>
					</tr>
					<tr></tr>
					<tr>
						<td style="vertical-align: middle;">审核处理： 
						       <c:if test="${bill.status eq '2' }">
								    <input id="pass_btn" class="btn btn-primary" type="submit" value="审核通过" onclick="audit('pass');" />&nbsp;	
									<input id="notpass_btn" class="btn btn-danger" type="button" value="审核驳回" onclick="audit('notpass');" />&nbsp;
								</c:if>
								<a href="${ctx}/cusInstorageAudit" class="btn btn-info" >返回</a>
								</td>
					</tr>
				</table>

			</div>

			<div class="tab-pane ${tab2}" id="tab2">
				<div style="height: 10px;"></div>

				<!-- 任务列表 -->
				<table class="table table-striped table-bordered table-condensed"
					style="width: 95%; margin-top: 0px;">
					<thead>
						<th style="vertical-align: middle; color: white; " colspan="7"
							bgcolor="#8EB4CB">任务列表</th>
					</thead>
					<tr>
						<th style="width: 100px;">车类型</th>
						<th style="width: 50px;">车编号</th>
						<th style="width: 250px;">装货信息</th>
						<th style="width: 80px;">指派人</th>
						<th style="width: 80px;">执行人</th>
						<th style="width: 80px;">任务状态</th>
						<th style="width: 80px;">操作</th>
					</tr>
					
					<c:forEach items="${taskList}" var="task">
					<tr >
					   <td>
					     <c:if test="${task.carType eq '1'}">整箱件</c:if>
					     <c:if test="${task.carType eq '2'}">散装件</c:if>
					     <c:if test="${task.carType eq '3'}">破损件</c:if>
					   </td>
					   <td>${task.carNo}号车</td>
					   <td><script type="text/javascript">document.write(formatCarInfo('${task.carInfo}'));</script></td>
					   <td id="commander_${task.id}" >${task.commander.realname}</td>
					   <td>
						   <select  id="worker_${task.id}" <c:if test="${task.status ne '0'}">disabled=disabled</c:if>  style="width: 130px;">
								<option value="">请选择员工...</option> 
								<c:forEach items="${userList}" var="user">
									<option value="${user.jobuuid}"
									  <c:if test="${ (task.worker ne null) && (task.worker.jobuuid eq user.jobuuid) }" >selected=selected</c:if>
									>${user.realname}</option>
								</c:forEach>
							</select> 
					   </td>
					   <td id="status_${task.id}">
					      <c:if test="${task.status eq '0'}">任务未指派</c:if>
					      <c:if test="${task.status eq '1'}">任务待执行...</c:if>
					      <c:if test="${task.status eq '3'}">任务完成</c:if>
					      <c:if test="${task.status eq '4'}">任务完成(有异常)</c:if>
					   </td>
					   <td>
					     <c:if test="${task.status eq '0'}">
					       <input class="btn btn-info" type="button" value="指派" id="btn_${task.id}" onclick="task('${task.id}');" />
					     </c:if>
					     <c:if test="${task.status ne '0'}">
						     <c:if test="${task.commandType eq '1'}">
						       <input class="btn" type="button" value="已指派" disabled="disabled" />
						     </c:if>
						     <c:if test="${task.commandType eq '2'}">
						       <input class="btn" type="button" value="已领取" disabled="disabled" />
						     </c:if>
					      </c:if>
					   </td>
					</tr>
					</c:forEach>

				</table>


			</div>


		</div>
		<!-- tab content -->

	</div>
	<!-- tab table -->

</body>
</html>
